package com.major.tree.avltree;

public class Node {
    private int value;
    private Node left;
    private Node right;

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public Node() {
    }

    public Node(int value) {
        this.value = value;
    }

    // 添加
    public void add(Node node) {

        if (node == null) {
            return;
        }

        // 向右添加
        if (node.value > value) {
            if (right == null) {
                right = node;
            } else {
                right.add(node);
            }
            // 向左添加
        } else {
            if (left == null) {
                left = node;
            } else {
                left.add(node);
            }
        }

        // 左子树高度 - 右子树高度 > 1 : 右旋转
        if (leftHeight() - rightHeight() > 1) {
            // 双旋转：如果左子结点的 右子树高度大于其左子树高度，则先对左子结点进行左旋转
            if (left.rightHeight() > left.leftHeight()) {
                // 先对左子结点进行左旋转
                left.leftRotate();
                // 再右旋转
                rightRotate();
            }else {
                // 直接右旋转
                rightRotate();
            }
        }
        // 左旋转
        if (rightHeight() - leftHeight() > 1) {
            if (right.leftHeight() > right.rightHeight()) {
                right.rightRotate();
            }
            leftRotate();
        }
    }

    // 右旋转
    private void rightRotate() {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

    // 左旋转
    private void leftRotate() {
        // 新结点(继承当前根结点)，作为未来根节点的左子结点
        Node newNode = new Node(value);
        // 给赋值
        newNode.left = left;
        newNode.right = right.left;

        // 右子结点 开始上位 成为根结点
        value = right.value;
        right = right.right;

        // 新结点成为左子结点
        left = newNode;
    }

    // 右子树高度
    public int rightHeight() {
        if (right == null) return 0;
        return right.height();
    }

    // 左子树高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    // 结点高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }


    // 中序遍历
    public void infixOrder() {

        if (left != null) {
            left.infixOrder();
        }
        System.out.println(this);
        if (right != null) {
            right.infixOrder();
        }


    }
}
